package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.ThumbDao;
import com.delivery.www.po.Thumb;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 12970
 */
public class ThumbDaoImpl implements ThumbDao {
    @Override
    public int doThumb(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, dishId};
        String sql = "insert into thumb (user_id,dish_id) values(?,?)";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public List<Thumb> getThumb(int userId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId};
        String sql = "select thumb_id,user_id,dish_id from thumb where user_id = ?";
        List<Thumb> thumbs = null;
        if (connection != null) {
            thumbs = BaseDao.getList(connection, Thumb.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return thumbs;
    }

    @Override
    public int cancelThumb(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, dishId};
        String sql = "delete from thumb where user_id = ? and dish_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public Thumb getOneThumb(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Thumb thumb = null;
        if (connection != null) {
            String sql = "select thumb_id,user_id,dish_id from thumb where user_id = ? and dish_id = ?";
            thumb = BaseDao.getForOne(connection, Thumb.class, sql, userId, dishId);
            JdbcUtils.closeResource(connection, null, null);
        }
        return thumb;
    }
}
